<template>
  <div class="app-container">
    <!-- height="300"固定表头，border显示竖直边框 ,stripe显示斑马纹 -->
    <div class="dict-box">
      <div class="dict-title">数字字典管理</div>
      <div class="add-box">
        <el-button type="primary" icon="el-icon-circle-plus-outline" @click="open">添加字典</el-button>
      </div>
      <div class="dict-table-div">
        <el-table v-loading="loading" :data="tableData" row-key="id" border style="width: 100%" :tree-props="{children: 'sub'}">
          <el-table-column prop="busi_name" label="数据名称" width="240" />
          <el-table-column prop="busi_value" label="数据值" width="280" />
          <el-table-column label="状态" width="160">
            <template slot-scope="scope">
              <span v-if="scope.row.state==1">有效</span>
              <span v-if="scope.row.state==0">无效</span>
            </template>
          </el-table-column>
          <el-table-column prop="sort_order" label="排序" width="160" />
          <el-table-column>
            <template slot-scope="scope">
              <el-button size="small" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button type="danger" size="small" icon="el-icon-delete" @click="handleDel(scope.row.id)">删除</el-button>
              <el-button v-if="scope.row.sub" type="primary" size="small" icon="el-icon-plus" @click="handleAdd(scope.row)">添加字典值</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="dict-pagination-div">
        <el-pagination
          background
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :pager-count="pageCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog class="dict-dialog" title="添加/修改数据字典" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="100px">
        <el-form-item label="数据名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入数据名称" />
        </el-form-item>
        <el-form-item label="数据值" prop="value">
          <el-input v-model="form.value" placeholder="请输入数据值" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-switch
            v-model="form.state"
            class="dict-switch"
            inactive-color="#aaa"
            inactive-text="无效"
            inactive-value="0"
            active-color="#5FB878"
            active-text="有效"
            active-value="1"
            :width="52"
            @change="change"
          />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" type="Number" placeholder="数值越大越靠前" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="comfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDictList, delDict, addSave, checkRepeti } from '@/api/dictionary'

export default {
  name: 'Index',
  data() {
    return {
      tableData: [], // 列表数据
      pageSize: 10, // 每页条数
      pageCount: 7, //
      pageSizes: [10, 20, 30, 40, 50, 100], // 个数选择器
      currentPage: 1, // 当前页码
      total: 0, // 总条数
      loading: false,
      form: {
        name: '', // 字典名称
        value: '', // 字典值
        state: '1', // 1是有效，0无效
        sort: '' // 排序
      },
      id: '',
      parentId: '', // 父级id
      dialogFormVisible: false // 弹出框
    }
  },
  computed: {},
  mounted() {
    this.getData(this.currentPage, this.pageSize)
  },
  methods: {
    /* eslint-disable */   //如果要eslint验证，请把这段代码删掉
    
    // 每页显示的条数
    handleSizeChange(val) {
      this.pageSize = val
      this.getData(1,val)
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.currentPage = 1
    },

    // 显示第几页
    handleCurrentChange(val) {
      this.currentPage = val
      // 切换页码时，要获取每页显示的条数
      this.getData(val,this.pageSize)
    },

    // 获取数据字典
    getData(page,size){
      getDictList({
        pageNumber: page,
        pageSize: size
      }).then(res=>{
        // console.log(res)
        if(res.state){
          this.tableData=res.data.page.list
          this.total=res.data.page.totalRow
        }else{
          this.$message.error(res.msg)
        }
      })
    },

    // 状态改变
    change(val) {
      // console.log(val)
      this.form.state=val
    },

    // 打开弹窗
    open() {
      this.dialogFormVisible=true
      this.form.name=''
      this.form.value=''
      this.form.state='1'
      this.form.sort=''
      this.id=''
      this.parentId='0'
      //console.log(this.form)
    },

    // 确定
    comfirm() {
      if(!this.form.name){
        this.$message({ showClose: true, message: '请输入数据名称', type: 'error' })
        return
      }
      if(!this.form.value){
        this.$message({ showClose: true, message: '请输入数据值', type: 'error' })
        return
      }
      if(!this.form.sort){
        this.$message({ showClose: true, message: '请输入排序值', type: 'error' })
        return
      }

      let data={
        'dict.id': this.id,
        'dict.busi_name': this.form.name,
        'dict.busi_value': this.form.value,
        'dict.state': this.form.state,
        'dict.sort_order': this.form.sort,
        'dict.parent_id': this.parentId
      }

      console.log(data)
      let checkName={
        'value': this.form.name, // 名称
        'dict_id': this.id,
        'pid': this.parentId, // 父级id
        'type':'busi_name', // busi_name（字段名称
      }

      let checkValue={
        'value': this.form.value, // 值
        'dict_id': this.id,
        'pid': this.parentId, // 父级id
        'type':'busi_value', // busi_value（字段值）
      }
      
      // 字典名验证
      checkRepeti(checkName).then(res=>{
        if(res.state){
          this.$message.error('字典名称重复!')
        }else{
          // 字典值验证
          checkRepeti(checkValue).then(res=>{
            if(res.state){
              this.$message.error('字典值重复!')
            }else{
              this.toAddSave(data)
            }
          })
        }
      })
    },

    // 添加或修改
    toAddSave(data){
      addSave(data).then(res=>{
        if(res.state){
          this.dialogFormVisible=false
          this.$message({message:res.msg,type:'success'})
          this.getData(this.currentPage,this.pageSize)
        }else{
          this.$message.error(res.msg)
        }
      })
    },

    // 编辑
    handleEdit(row) {
      console.log(row)
      this.dialogFormVisible=true
      this.form.name=row.busi_name
      this.form.value=row.busi_value
      this.form.state=row.state+''
      this.form.sort=row.sort_order
      this.id=row.id
      this.parentId=row.parent_id
    },

    // 删除
    handleDel(id) {
      console.log(id)
      this.$confirm('你是否确定删除这条信息?', '询问', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delDict({id:id}).then(res=>{
          if(res.state){
            this.$message({message:res.msg,type:'success'})
            this.getData(this.currentPage,this.pageSize)
          }else{
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {})
    },

    // 添加
    handleAdd(row) {
      console.log(row)
      this.dialogFormVisible=true
      this.form.name=''
      this.form.value=''
      this.form.state='1'
      this.form.sort=''
      this.id=''
      this.parentId=row.id
    },

  }
}
</script>

<style lang="scss" scoped>
.dict-box{
  border: 1px solid #f2f2f2;
  border-radius: 3px;
  box-shadow: 0 1px 1px 0 rgba(0,0,0,.12);
  background: #fff;

  .dict-title{
    font-size: 15px;
    color: #333;
    padding: 15px;
    border-bottom: 1px solid #f2f2f2;
  }

  .add-box{
    padding: 15px;
  }

  .dict-table-div{
    padding: 15px;
  }

  .dict-pagination-div{
    display: flex;
    align-items: center;
    // justify-content: flex-end; //右对齐
    margin: 10px 15px 15px 15px;
    padding: 10px 0px;
    border-top: 1px solid #dfe6ec;
  }

}
</style>

<style lang="scss"> // element-ui样式修改
.dict-dialog{
  .dict-switch{
    position: relative;
    .el-switch__label {
      position: absolute;
      display: none;
      color: #fff;
    }

    /*打开时文字位置设置*/
    .el-switch__label--right {
      z-index: 1;
      left: -6px;
    }

    /*关闭时文字位置设置*/
    .el-switch__label--left {
      z-index: 1;
      right: -5px;
    }

    /*显示文字*/
    .el-switch__label.is-active {
      display: block;
    }

  }
}

</style>
